/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import org.jsoup.Jsoup;

import java.io.FileOutputStream;
import java.util.HashMap;

/**
 * <p>Project: javaseapp - Qr1
 * <p>Powered by webrx On 2021-08-17 08:53:09
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Qr1 {
    public static void main(String[] args) {
        try {
            //编码，默认是不支持中文
            var bm = new MultiFormatWriter().encode("13014577033", BarcodeFormat.QR_CODE, 200, 200);
            MatrixToImageWriter.writeToStream(bm, "png", new FileOutputStream("my2.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //翻译结果，生成二维码，支持中文
        try {
            String w = "java";
            String u = "https://cn.bing.com/dict/search?q=" + w;
            String o = Jsoup.connect(u).get().select("span[class=def b_regtxt]").get(0).text();
            System.out.println(o);
            String text = String.format("%s : %s", w, o);
            var hints = new HashMap<EncodeHintType, Object>();
            hints.put(EncodeHintType.MARGIN, 2);
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            var cnbm = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 200, 200, hints);
            MatrixToImageWriter.writeToStream(cnbm, "png", new FileOutputStream("dict/" + w + ".png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
